export default {
    name: 'RegisterView',
    data() {
        return {
            ruleForm: {
                name: '',
                mobile: '',
                pwd: '',
                repwd: '',
                code: '',
                delivery: false,
            },
            rules: {
                name: [
                    { required: true, message: '请输入用户名', trigger: 'blur' },
                    { pattern: /^[a-zA-Z][a-zA-Z0-9]{3,14}$/, message: '以字符开头,必须包含数字,长度4-15', trigger: 'blur' }
                ],
                mobile: [
                    { required: true, message: '请输入手机号码', trigger: 'blur' },
                    { pattern: /^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/, message: '以1开头的11位数字', trigger: 'blur' }
                ],
                pwd: [
                    { required: true, message: '请设置密码', trigger: 'blur' },
                    { pattern: /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{6,16}$/, message: '必须包含大小写字母、数字,且长度为6到16位', trigger: 'blur' }
                ],
                repwd: [
                    { required: true, message: '请确认密码', trigger: 'blur' },
                    {
                        validator: (rule, value, callback) => {
                            if (value === '') {
                                callback(new Error('请再次输入密码'))
                            } else if (value !== this.ruleForm.pwd) {
                                callback(new Error('两次输入密码不一致'))
                            } else {
                                callback()
                            }
                        },
                        trigger: 'blur'
                    }
                ],
                code: [
                    {}
                ]
            }
        };
    },
    methods: {
        register() {
            let name = /^[a-zA-Z][a-zA-Z0-9]{3,14}$/
            let mobile = /^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/
            let pwd = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{6,16}$/
            if (this.ruleForm.name.length < 1 || this.ruleForm.mobile.length < 1 || this.ruleForm.pwd.length < 1
                || this.ruleForm.repwd.length < 1) {
                this.$message({
                    message: '请填写完整的信息',
                    type: 'error',
                    duration: 1000
                });
                return
            } else if (!name.test(this.ruleForm.name) || !mobile.test(this.ruleForm.mobile) || !pwd.test(this.ruleForm.pwd)
                || !pwd.test(this.ruleForm.pwd) || this.ruleForm.pwd != this.ruleForm.repwd) {
                this.$message({
                    message: '请填写符合要求的信息',
                    type: 'error',
                    duration: 1000
                });
                return
            }
            if (this.ruleForm.delivery == true) {
                this.axios.post('http://localhost:3000/api/register', { userName: this.ruleForm.name, userPwd: this.ruleForm.pwd, userMobile: this.ruleForm.mobile })
                    .then(res => {
                        if (res.data.message == 'ok') {
                            localStorage.setItem('token', res.data.user)
                            //保存当前登录用户完整信息
                            localStorage.setItem('currentUser', JSON.stringify(res.data.data))
                            location.href = 'http://localhost:8080/login'
                        }
                    })
            } else {
                this.$message({
                    message: '请先阅读并同意《用户注册协议》！',
                    type: 'error',
                    duration: 1000
                });
            }
        },
        goindex() {
            this.$router.push('/index')
        },
        gologin() {
            this.$router.push('/login')
        }
    }
}